<?php
namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\ModuleActions;
use cms\modules\v1\models\notice\Notice;
use common\models\Functions;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

class MessageController extends DefaultController
{
    public $modelClass = 'cms\modules\v1\models\notice\Notice';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),
                'header' => 'access-token',
                'optional' => [
                ]
            ]
        ]);
    }

    //列表
    public function actionList($page = 1, $size = 20) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new Notice())->getList($page, $size);
        }
    }

    // 添加系统消息
    public function actionAddEdit() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        $model = new Notice();
        if (Yii::$app->request->isPost) {
            $model->setScenario('add');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->addMessage();
            }
        }
    }

    // 发送消息
    public function actionSend() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        $model = new Notice();
        if (Yii::$app->request->isPost) {
            return $model->sendIm();
        }
    }


}


